// pages/user/index.js
import {request} from "../../utils/util";
const app =getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
    },
    /*queryparm:{
        encryptedData:'',
        rawData:'',
        iv:'',
        signature:'',
        code:''
    },*/
    //获取权限
    handlogin(){
        wx.getUserProfile({
            desc:'获取用户信息',
            success:(res) => {
                /*this.queryparm.encryptedData=res.encryptedData;
                this.queryparm.rawData=res.rawData;
                this.queryparm.iv=res.iv;
                this.queryparm.signature=res.signature;
                let that=this
                //官网登入 返回code
                wx.login({
                    async success(lores) {
                        //console.log(this);
                        //设置code参数
                        that.queryparm.code = lores.code;
                        //请求后台获取token
                        let token=await request({
                            url: `${app.globalData.url}users/wxlogin`,
                            data: that.queryparm,
                            methods: 'POST'
                        });
                        console.log(token);
                    }
                })*/
                //保存一个时间的属性
                //res.userInfo.time = Date.now();
                this.authnumber((retval) => {
                    if(retval.length!=0){
                        res.userInfo.telephone=retval;
                        //保存用户信息
                        wx.setStorageSync('userinfo', res.userInfo);
                    }
                });
                /*//过期时间
                let gqtime = res.userInfo.time + 1000 * 60 * 5;
                wx.setStorageSync('lasttime', gqtime);
                let that = this;
                let t = setInterval(function () {
                    let lat = wx.getStorageSync('lasttime');
                    let diff = lat - Date.now();
                    //console.log(diff);
                    if (diff <= 0) {
                        clearInterval(t);
                        that.setData({
                            userinfo: null,
                            collnumber: 0,
                            hosnumber: 0
                        })
                        //移到用户信息
                        wx.removeStorageSync('userinfo');
                        //移除时间
                        wx.removeStorageSync('lasttime');
                    }
                }, 100)*/
                console.log(res.userInfo);
                let col = wx.getStorageSync('collect') || [];
                let hos = wx.getStorageSync('hostoryshop') || [];
                this.setData({
                    userinfo: res.userInfo,
                    collnumber: col.length,
                    hosnumber: hos.length
                });
            },fail(err){
                wx.showToast({
                    title: '授权失败',
                    icon:"error",
                    mask:true
                })
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.jueduserinfo();
    },

    //判断用户信息
    jueduserinfo(){
        let usinfo=wx.getStorageSync('userinfo');
        //为空的时
        if(!usinfo){
            this.setData({
                userinfo:null,
                collnumber:0,
                hosnumber:0
            })
        }else{
            //取出收藏
            let col=wx.getStorageSync('collect');
            //取出足迹
            let hos=wx.getStorageSync('hostoryshop');
            this.setData({
                userinfo:usinfo,
                collnumber:col.length,
                hosnumber:hos.length
            })
        }
    },
    //推荐给别人
    shar(){

    },
    //自动生成电话
    authnumber(callback){
        let clau=setInterval(()=>{
            var nuM = [];
            //通过随机函数生成电话号码,并存入数组中
            for (var i = 0; i < 11; i++) {
                var num = Math.floor(Math.random() * 10);
                nuM.unshift(num);

            }
            //把数组中的逗号通过join方法消除掉
            nuM = nuM.join("");
            //通过正则判断号码是否为正常电话号码
            if ((/^1[34578]\d{9}$/.test(nuM))) {
                // alert("找到了");
                nuM = nuM.toString();
                clearInterval(clau);
                callback(nuM);
            }
        },100);
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
